package com.cloudptt.api.product.func.lbs.server;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.cloudptt.api.models.lbs.LocationData;
import com.cloudptt.api.models.lbs.VitalData;
import com.cloudptt.api.product.config.lbs.LocationConfig;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.util.GPSUtil;

import android.content.Context;

public class BaiduLocServer extends AbstractLocServer implements BDLocationListener{

	private static final String TAG = "BaiduLocServer";
	
	public LocationClient baiduLocClient;
	
	private LocationConfig curLocalConfig = null;

	private long lastestRptTime = 0;
	private LocationData curLocation = new LocationData(0,0);
	
	public void startLBS(Context ctx,LocationConfig localConfig) 
	{
		Log.i(TAG, "startLBS localConfig = " + localConfig);
		if(localConfig == null
				|| !localConfig.isOpenLocation())
		{
			return;
		}
		this.curLocalConfig = localConfig.clone();

		lastestRptTime = 0;
		
		baiduLocClient = new LocationClient(ctx);
		baiduLocClient.registerLocationListener(this);
		LocationClientOption option = new LocationClientOption();

		int interval = localConfig.getTimeInterval();	

		// 定位初始化
		option.setCoorType("bd09ll"); // 设置坐标类型
		Log.i(TAG, "startBaidu()  interval: "+ interval);
		option.setScanSpan(interval * 1000);// 可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
		option.setOpenGps(true);
		baiduLocClient.setLocOption(option);
		baiduLocClient.start();
	}

	@Override
	public void onReceiveLocation(BDLocation location) {

		Log.i(TAG, "onReceiveLocation Baidu Latitude = " + location.getLatitude() + " Longitude = "+ location.getLongitude()); 
		if(lastestRptTime!=0 && !curLocation.isValid())
		{
			double lastLattitude = curLocation.getLattitude();
			double lastLongitude = curLocation.getLongitude();
			double distance = GPSUtil.GetShortDistance(location.getLatitude(), location.getLongitude(),lastLattitude,lastLongitude);
			boolean isInvalid = isInvalidReportLocationData(curLocalConfig,lastestRptTime,distance);
			if(isInvalid)
			{
				Log.i(TAG, "onReceiveLocation Baidu Invalid"); 
				return;
			}
		}
		lastestRptTime = System.currentTimeMillis();
		curLocation.updateLocationData(location.getLatitude(), location.getLongitude());
	}
	public void updateConfig(Context ctx,LocationConfig localConfig)
	{
		Log.i(TAG, "updateConfig localConfig = " + localConfig);
		
		boolean isSame = curLocalConfig.isSameInterval(localConfig);
		Log.i(TAG, "updateConfig isSame = " + isSame);
		if(!isSame)
		{
			curLocalConfig = localConfig.clone();
			stopLBS();
			startLBS(ctx,localConfig);
		}
	}
	
	public void stopLBS()
	{	
		Log.i(TAG, "stopLBS localConfig = " + curLocalConfig);
		if(curLocalConfig == null
				|| !curLocalConfig.isOpenLocation())
		{
			return;
		}
		
		if(baiduLocClient!=null)
		{
			baiduLocClient.unRegisterLocationListener(this);
			baiduLocClient.stop();
		}
		
		lastestRptTime 	= 0;
	}
	public LocationData getCurLocation() {
		return curLocation;
	}
}
